package com.atguigu.gmall.cart.rest;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.config.interceptor.UserAuthContext;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.entity.CartInfo;
import com.atguigu.gmall.model.cart.entity.UserAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @Autowired
    UserAuthContext userAuthContext;

    @GetMapping("/cartList")
    public Result<List<CartInfo>> cartList() {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        List<CartInfo> cartInfoList = cartService.getCartList(key);
        return Result.ok(cartInfoList);
    }

    @GetMapping("/userCartList")
    public Result<List<CartInfo>> userCartList() {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        List<CartInfo> cartInfoList = cartService.getUserCartList(key);
        return Result.ok(cartInfoList);
    }

    @DeleteMapping("/deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId) {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        cartService.deleteCart(key,skuId);
        return Result.ok();
    }
    @PostMapping("/addToCart/{skuId}/{num}")
    public Result addToCart(@PathVariable Long skuId, @PathVariable Integer num) {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        cartService.addToCart(key,skuId,num);
        return Result.ok();
    }

    @GetMapping("/checkCart/{skuId}/{check}")
    public Result checkCart(@PathVariable Long skuId, @PathVariable Integer check) {
        UserAuth userAuth = userAuthContext.getUserAuth();
        String key = cartService.decisionKey(userAuth);
        cartService.checkCart(key,skuId,check);
        return Result.ok();
    }

}
